<?php


namespace App\Http\Controllers\Admin;
use App\Http\Controllers\BaseController;
use App\Models\Config;
use Illuminate\Http\Request;

class ConfigController extends BaseController
{


    public function index(Request $request) {
        $key = $request->input('key');
        $list = Config::when($key, function ($q) use($key){
            $q->where('key', $key);
        })->paginate(per_page());
        return $this->paginate($list);
    }

    public function info(Request $request) {
        $key = $request->input('key', 'site');
        $info = Config::where('key', $key)->first();
        $info->value = json_decode($info->value, true);
        return $this->success($info);
    }

    /**
     * 修改
     * @param Request $request
     * @return mixed
     * @throws \Illuminate\Validation\ValidationException
     */
    public function store(Request $request) {
        $this->validate($request, [
            'key' => 'required',
            'value' => 'required'
            ]);
        $input = $request->input();
        $key = $request->input('key');

        $model = Config::where('key', $key)->first();
        $model->value = $input['value'];
        $model->save();
        Config::clearCache();
        return $this->success([]);
    }

}

